#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include "activity/activities.h"
#include "gui/gui.h"
#include "utils.h"
#include "lang.h"

#include "about_text.c"

static int onStartActivity(GUI_Activity *activity);
static int onFinishActivity(GUI_Activity *activity);
static int onDrawActivity(GUI_Activity *activity);
static int onCtrlActivity(GUI_Activity *activity);

static GUI_ButtonInstruction button_instructions[] = {
    {LANG_LOCAL_BUTTON_CANCEL, LANG_BACK, 1},
    {LANG_NULL, LANG_NULL, 1},
};

GUI_Activity about_activity = {
    LANG_ABOUT_TITLE,    // Title
    button_instructions, // Button instructions
    NULL,                // Wallpaper
    0,                   // Disable draw statusbar
    0,                   // Disable home event
    onStartActivity,     // Start callback
    onFinishActivity,    // Finish callback
    NULL,                // Before draw callback
    onDrawActivity,      // Draw callback
    NULL,                // After draw callback
    onCtrlActivity,      // Ctrl callback
    NULL,                // Event callback
    NULL,                // User data
};

#define LISTVIEW_PADDING_L 6
#define LISTVIEW_PADDING_T 6
#define ITEMVIEW_PADDING_L 4
#define ITEMVIEW_PADDING_T 3

#define TEXT_COLOR COLOR_ORANGE

static Layout *about_layout = NULL;
static ListView *about_listView = NULL;

static int refreshListView()
{
    ItemView *itemView;
    int i;

    if (!about_listView)
        return -1;

    ListViewEmptyItemViews(about_listView);

    for (i = 0; i < N_ABOUT_TEXTS; ++i)
    {
        itemView = NewItemView();
        if (!itemView)
            break;

        ItemViewSetLayoutSize(itemView, TYPE_LAYOUT_SIZE_MATH_PARENT, TYPE_LAYOUT_SIZE_WRAP_CONTENT);
        ItemViewSetPadding(itemView, ITEMVIEW_PADDING_L, ITEMVIEW_PADDING_L, ITEMVIEW_PADDING_T, ITEMVIEW_PADDING_T);
        ItemViewSetNameTextColor(itemView, TEXT_COLOR);
        ItemViewSetNameText(itemView, about_texts[i]);
        ListViewAddItemView(about_listView, itemView);
    }

    ListViewMeasure(about_listView);

    return i;
}

static int createContentView()
{
    int layout_x = 0, layout_y = 0;
    int available_w = 0, available_h = 0;

    if (about_layout)
        return 0;

    GUI_GetActivityLayoutPosition(&about_activity, &layout_x, &layout_y);
    GUI_GetActivityAvailableSize(&about_activity, &available_w, &available_h);

    about_layout = NewLayout();
    if (!about_layout)
        return -1;
    LayoutSetLayoutSize(about_layout, TYPE_LAYOUT_SIZE_MATH_PARENT, TYPE_LAYOUT_SIZE_MATH_PARENT);
    LayoutSetLayoutPosition(about_layout, layout_x, layout_y);
    LayoutSetLayoutAvailableSize(about_layout, available_w, available_h);
    LayoutSetOrientation(about_layout, TYPE_LAYOUT_ORIENTATION_VERTICAL);
    LayoutSetPadding(about_layout, GUI_DEF_MAIN_LAYOUT_PADDING, GUI_DEF_MAIN_LAYOUT_PADDING, GUI_DEF_MAIN_LAYOUT_PADDING, GUI_DEF_MAIN_LAYOUT_PADDING);

    about_listView = NewListView();
    if (!about_listView)
        return -1;
    ListViewSetLayoutSize(about_listView, TYPE_LAYOUT_SIZE_MATH_PARENT, TYPE_LAYOUT_SIZE_MATH_PARENT);
    ListViewSetOrientation(about_listView, TYPE_LAYOUT_ORIENTATION_VERTICAL);
    ListViewSetPadding(about_listView, LISTVIEW_PADDING_L, LISTVIEW_PADDING_L, LISTVIEW_PADDING_T, LISTVIEW_PADDING_T);
    ListViewSetBgColor(about_listView, GUI_DEF_COLOR_BG);
    LayoutAddView(about_layout, about_listView);

    LayoutMeasure(about_layout);

    return 0;
}

static int destroyContentView()
{
    LayoutDestroy(about_layout);
    about_layout = NULL;
    about_listView = NULL;

    return 0;
}

static int onStartActivity(GUI_Activity *activity)
{
    about_activity.wallpaper = GUI_GetImage(ID_GUI_IMAGE_WALLPAPER);
    createContentView();
    refreshListView();
    return 0;
}

static int onFinishActivity(GUI_Activity *activity)
{
    destroyContentView();
    return 0;
}

static int onDrawActivity(GUI_Activity *activity)
{
    LayoutDraw(about_layout);
    return 0;
}

static int onCtrlActivity(GUI_Activity *activity)
{
    if (hold_pad[PAD_UP] || hold2_pad[PAD_LEFT_ANALOG_UP])
    {
        ListViewMoveTopPos(about_listView, TYPE_MOVE_UP, 1);
    }
    else if (hold_pad[PAD_DOWN] || hold2_pad[PAD_LEFT_ANALOG_DOWN])
    {
        ListViewMoveTopPos(about_listView, TYPE_MOVE_DOWN, 1);
    }
    else if (hold_pad[PAD_LEFT])
    {
        ListViewMoveTopPos(about_listView, TYPE_MOVE_LEFT, 1);
    }
    else if (hold_pad[PAD_RIGHT])
    {
        ListViewMoveTopPos(about_listView, TYPE_MOVE_RIGHT, 1);
    }
    else if (released_pad[PAD_CANCEL])
    {
        GUI_FinishActivity(activity);
    }

    return 0;
}
